//
//  KFDBindEmailViewController.m
//  KFD
//
//  Created by JIRENTIANXIANG on 7/20/15.
//  Copyright © 2015 kfd. All rights reserved.
//

#import "KFDBindEmailViewController.h"
#import "KFDAccountManager.h"
#import "JTToast.h"
#import "ui_config.h"
#import "KFDUIConfigurator.h"

@interface KFDBindEmailViewController ()

@property (weak, nonatomic) IBOutlet UILabel *fillinLabel;
@property (weak, nonatomic) IBOutlet UILabel *verifyLabel;
@property (weak, nonatomic) IBOutlet UITextField *emailField;

@end

@implementation KFDBindEmailViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    KFDUIConfigurator* uiConfigurator=[KFDUIConfigurator configuratorForViewController:self];
    [uiConfigurator configureNavigationBarWithBackAction:@selector(goBack)];
    self.title=@"填写邮箱";
}

-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)saveButtonPressed:(id)sender {
    NSString* email=self.emailField.text;
    KFDAccountManager* accountManager=[KFDAccountManager sharedManager];
    if ([accountManager checkEmail:email illegalInputHandler:^(NSString *message) {
        [[JTToast makeText:message] showInView:self.view size:CGSizeMake(message.length*20, 40)];
    }]) {
        [accountManager bindEmail:email successHandler:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.emailField resignFirstResponder];
                self.fillinLabel.textColor=KFD_LIGHT_GRAY;
                self.verifyLabel.textColor=KFD_RED;
            });
        } failulreHandler:^(KFDResponseBody *response) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.emailField resignFirstResponder];
                [[JTToast makeText:response.message] showInView:self.view size:CGSizeMake(response.message.length*20, 40)];
            });
        }];
    }
}

@end
